package com.ruoyi.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.domain.TModelcamgroup;
import com.ruoyi.domain.TModelfloorgroup;
import com.ruoyi.mapper.TModelcamgroupMapper;
import com.ruoyi.mapper.TModelfloorgroupMapper;
import com.ruoyi.resp.CameraInfo4;
import com.ruoyi.resp.MonitorStatis;
import com.ruoyi.resp.MonitoringTeam;
import com.ruoyi.system.domain.TtsCameraInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.TtsCameraInfoMapper;
import com.ruoyi.service.ITtsCameraInfoService;

/**
 * 摄像头信息Service业务层处理
 * 
 * @author li
 * @date 2025-08-18
 */
@Service
public class TtsCameraInfoServiceImpl implements ITtsCameraInfoService 
{
    @Autowired
    private TtsCameraInfoMapper ttsCameraInfoMapper;
    @Autowired
    private TModelfloorgroupMapper modelfloorgroupMapper;
    @Autowired
    private TModelcamgroupMapper modelcamgroupMapper;

    /**
     * 查询摄像头信息
     * 
     * @param id 摄像头信息主键
     * @return 摄像头信息
     */
    @Override
    public TtsCameraInfo selectTtsCameraInfoById(Long id)
    {
        return ttsCameraInfoMapper.selectTtsCameraInfoById(id);
    }

    /**
     * 查询摄像头信息列表
     * 
     * @param ttsCameraInfo 摄像头信息
     * @return 摄像头信息
     */
    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<TtsCameraInfo> selectTtsCameraInfoList(TtsCameraInfo ttsCameraInfo)
    {
        return ttsCameraInfoMapper.selectTtsCameraInfoList(ttsCameraInfo);
    }

    /**
     * 新增摄像头信息
     * 
     * @param ttsCameraInfo 摄像头信息
     * @return 结果
     */
    @Override
    public int insertTtsCameraInfo(TtsCameraInfo ttsCameraInfo)
    {
        ttsCameraInfo.setCreateTime(DateUtils.getNowDate());
        return ttsCameraInfoMapper.insertTtsCameraInfo(ttsCameraInfo);
    }

    /**
     * 修改摄像头信息
     * 
     * @param ttsCameraInfo 摄像头信息
     * @return 结果
     */
    @Override
    public int updateTtsCameraInfo(TtsCameraInfo ttsCameraInfo)
    {
        ttsCameraInfo.setUpdateTime(DateUtils.getNowDate());
        return ttsCameraInfoMapper.updateTtsCameraInfo(ttsCameraInfo);
    }

    /**
     * 批量删除摄像头信息
     * 
     * @param ids 需要删除的摄像头信息主键
     * @return 结果
     */
    @Override
    public int deleteTtsCameraInfoByIds(Long[] ids)
    {
        return ttsCameraInfoMapper.deleteTtsCameraInfoByIds(ids);
    }

    /**
     * 删除摄像头信息信息
     * 
     * @param id 摄像头信息主键
     * @return 结果
     */
    @Override
    public int deleteTtsCameraInfoById(Long id)
    {
        return ttsCameraInfoMapper.deleteTtsCameraInfoById(id);
    }

    @Override
    public List<Map<String, Object>> selectTtsCameraInfoListScheduled() {
        return ttsCameraInfoMapper.selectTtsCameraInfoListScheduled();
    }

    @Override
    public List<MonitoringTeam> selectTtsCameraInfoByGetPlanList() {
        List<MonitoringTeam> result = new ArrayList<>();
        // 第一步：获取所有天童寺的监控预案
        TModelfloorgroup query = new TModelfloorgroup();
        query.setSceneId(81L);
        List<TModelfloorgroup> plans = modelfloorgroupMapper.selectTModelfloorgroupList(query);
        // 遍历每个监控预案
        for (TModelfloorgroup plan : plans) {
            // 为每个预案创建一个监控团队对象
            MonitoringTeam team = new MonitoringTeam();
            team.setName(plan.getName());
            team.setRtspList(new ArrayList<>());
            // 第二步：获取当前预案的所有监控路线
            TModelcamgroup camGroupQuery = new TModelcamgroup();
            camGroupQuery.setFId(Long.valueOf(plan.getId()));
            List<TModelcamgroup> camGroups = modelcamgroupMapper.selectTModelcamgroupList(camGroupQuery);
            // 遍历每个监控路线
            for (TModelcamgroup camGroup : camGroups) {
                // 获取路线中的所有摄像机代码
                String[] cameraCodes = camGroup.getHostCode().split(",");
                // 遍历每个摄像机代码
                for (String cameraCode : cameraCodes) {
                    // 查询摄像机信息
                    TtsCameraInfo camera = ttsCameraInfoMapper.selectTtsCameraInfoByIndexCode(cameraCode.trim());
                    if (camera != null && camera.getRtsp() != null && !camera.getRtsp().isEmpty()) {
                        // 创建 RTSP 对象并添加到团队列表
                        MonitoringTeam.rtsp rtsp = new MonitoringTeam.rtsp();
                        rtsp.setRtsp(camera.getRtsp());
                        team.getRtspList().add(rtsp);
                    }
                }
            }
            // 将当前团队添加到结果列表
            result.add(team);
        }
        return result;
    }

    @Override
    public MonitorStatis selectTtsCameraInfoCamStatis() {
        //返回结果数据
        MonitorStatis monitorStats = new MonitorStatis();
        TtsCameraInfo ttsCameraInfo = new TtsCameraInfo();
        ttsCameraInfo.setDeleted(Long.valueOf(0));
        List<TtsCameraInfo> ttsCameraInfos = ttsCameraInfoMapper.selectTtsCameraInfoList(ttsCameraInfo);
        // 初始化计数器
        int onlineCount = 0;
        int offlineCount = 0;
        // 遍历摄像头列表，根据 state 字段统计
        for (TtsCameraInfo camera : ttsCameraInfos) {
            if (camera.getState().equals("在线")) {
                onlineCount++;
            } else if (!camera.getState().equals("在线")) {
                offlineCount++;
            }
        }
        // 设置统计结果
        int totalCount = ttsCameraInfos.size();
        monitorStats.setCameraCount(totalCount);
        monitorStats.setOnlineCount(onlineCount);
        monitorStats.setOfflineCount(offlineCount);
        // 计算完好率
        if (totalCount > 0) {
            // 计算百分比并四舍五入取整
            int onlineRate = (int) Math.round((double) onlineCount / totalCount * 100);
            monitorStats.setOnlineRate(onlineRate);
        } else {
            monitorStats.setOnlineRate(0);
        }
        return monitorStats;
    }

    @Override
    public List<CameraInfo4> selectTtsCameraInfo4() {
        List<CameraInfo4> result = new ArrayList<>();
        List<TtsCameraInfo> ttsCameraInfos = ttsCameraInfoMapper.selectTtsCameraInfoList(null);
        int count = 0;
        for (TtsCameraInfo ttsCameraInfo : ttsCameraInfos) {
            // 只处理前四条
            if (count >= 4) {
                break;
            }
            CameraInfo4 cameraInfo4 = new CameraInfo4();
            cameraInfo4.setCameraId(String.valueOf(ttsCameraInfo.getId()));
            cameraInfo4.setCameraCode(ttsCameraInfo.getIndexCode());
            cameraInfo4.setCameraName(ttsCameraInfo.getName());
            cameraInfo4.setRtsp(ttsCameraInfo.getRtsp());
            cameraInfo4.setState(ttsCameraInfo.getState());
            result.add(cameraInfo4);
            count++;
        }
        //其中根据只保留前四条数据
        return result.stream().limit(4).collect(Collectors.toList());
    }
}
